import { getDeviceParameterDb } from '../lib/device-parameter-database';

// 简写名称到实际表名的映射
const shortNameToTableName: Record<string, string> = {
  '表 C.2': 'table_c2',
  '表C.2': 'table_c2',
  '表 C.4': 'table_c4',
  '表C.4': 'table_c4',
  '表 C.6': 'table_c6',
  '表C.6': 'table_c6',
  '表 C.14': 'table_c14',
  '表C.14': 'table_c14',
  '表 C.18': 'table_c18',
  '表C.18': 'table_c18',
  '表 C.27': 'table_c27',
  '表C.27': 'table_c27',
  '表 C.30': 'table_c30',
  '表C.30': 'table_c30',
  '表 C.36': 'table_c36',
  '表C.36': 'table_c36',
  '表 C.81': 'table_c81',
  '表C.81': 'table_c81',
  '表 C.109-1': 'table_c109_1',
  '表C.109-1': 'table_c109_1',
  '表 C.144': 'table_c144',
  '表C.144': 'table_c144',
  '表 C.150': 'table_c150',
  '表C.150': 'table_c150',
  '表 C.173': 'table_c173',
  '表C.173': 'table_c173',
  '表 C.174': 'table_c174',
  '表C.174': 'table_c174',
  '表 C.178': 'table_c178',
  '表C.178': 'table_c178',
  '表 C.179': 'table_c179',
  '表C.179': 'table_c179',
  '表 C.183': 'table_c183',
  '表C.183': 'table_c183',
  '表 C.201': 'table_c201',
  '表C.201': 'table_c201',
  '表 C.202': 'table_c202',
  '表C.202': 'table_c202',
  '表 C.203': 'table_c203',
  '表C.203': 'table_c203',
  '表 C.204': 'table_c204',
  '表C.204': 'table_c204',
  '表 C.207': 'table_c207',
  '表C.207': 'table_c207',
  '表 C.208': 'table_c208',
  '表C.208': 'table_c208',
  '表 C.212': 'table_c212',
  '表C.212': 'table_c212',
  '表 C.252': 'table_c252',
  '表C.252': 'table_c252',
  '表 C.254': 'table_c254',
  '表C.254': 'table_c254',
  '表 C.255': 'table_c255',
  '表C.255': 'table_c255',
  '表 C.259': 'table_c259',
  '表C.259': 'table_c259',
  '表 C.260': 'table_c260',
  '表C.260': 'table_c260',
  '表 C.264': 'table_c264',
  '表C.264': 'table_c264',
  '表 C.265': 'table_c265',
  '表C.265': 'table_c265',
  '表 C.271': 'table_c271',
  '表C.271': 'table_c271',
  '表 C.273': 'table_c273',
  '表C.273': 'table_c273',
  '表 C.277': 'table_c277',
  '表C.277': 'table_c277',
  '表 C.279': 'table_c279',
  '表C.279': 'table_c279',
  '表 C.280': 'table_c280',
  '表C.280': 'table_c280',
  '表 C.281': 'table_c281',
  '表C.281': 'table_c281',
  '表 C.284': 'table_c284',
  '表C.284': 'table_c284',
  '表 C.285': 'table_c285',
  '表C.285': 'table_c285',
  '表 C.305': 'table_c305',
  '表C.305': 'table_c305',
  '表 C.306': 'table_c306',
  '表C.306': 'table_c306',
  '表 C.313': 'table_c313',
  '表C.313': 'table_c313',
  '表 C.315': 'table_c315',
  '表C.315': 'table_c315',
  '表 C.319': 'table_c319',
  '表C.319': 'table_c319',
  '表 C.320': 'table_c320',
  '表C.320': 'table_c320',
  '表 C.324': 'table_c324',
  '表C.324': 'table_c324',
  '表 C.339': 'table_c339',
  '表C.339': 'table_c339',
  '表 C.341': 'table_c341',
  '表C.341': 'table_c341',
  '表 C.343': 'table_c343',
  '表C.343': 'table_c343',
  '表 C.348': 'table_c348',
  '表C.348': 'table_c348',
  '表 C.363': 'table_c363',
  '表C.363': 'table_c363',
  '表 C.367': 'table_c367',
  '表C.367': 'table_c367',
  '表 C.368': 'table_c368',
  '表C.368': 'table_c368',
  '表 C.376': 'table_c376',
  '表C.376': 'table_c376',
  '表 C.385': 'table_c385',
  '表C.385': 'table_c385',
  '表 C.386': 'table_c386',
  '表C.386': 'table_c386',
  '表 C.393': 'table_c393',
  '表C.393': 'table_c393',
  '表 C.394': 'table_c394',
  '表C.394': 'table_c394',
  '表 C.395': 'table_c395',
  '表C.395': 'table_c395',
  '表 C.397': 'table_c397',
  '表C.397': 'table_c397',
  '表 C.413': 'table_c413',
  '表C.413': 'table_c413',
  '表 C.416': 'table_c416',
  '表C.416': 'table_c416',
  '表 C.418': 'table_c418',
  '表C.418': 'table_c418',
  '表 C.419': 'table_c419',
  '表C.419': 'table_c419',
  '表 C.421': 'table_c421',
  '表C.421': 'table_c421',
  '表 C.422': 'table_c422',
  '表C.422': 'table_c422',
  '表 C.425': 'table_c425',
  '表C.425': 'table_c425',
  '表 C.431': 'table_c431',
  '表C.431': 'table_c431',
  '表 C.432': 'table_c432',
  '表C.432': 'table_c432',
  '表 C.433': 'table_c433',
  '表C.433': 'table_c433',
  '表 C.434': 'table_c434',
  '表C.434': 'table_c434',
  '表 C.469': 'table_c469',
  '表C.469': 'table_c469',
  '表 C.470': 'table_c470',
  '表C.470': 'table_c470',
  '表 C.483': 'table_c483',
  '表C.483': 'table_c483',
  '表 C.485': 'table_c485',
  '表C.485': 'table_c485',
  '表 C.494': 'table_c494',
  '表C.494': 'table_c494',
  '表 C.496': 'table_c496',
  '表C.496': 'table_c496',
  '表 C.497': 'table_c497',
  '表C.497': 'table_c497'
};

async function fixDeviceMappingsToRealTables() {
  try {
    const db = getDeviceParameterDb();
    const deviceParameterDb = db as any;

    console.log('开始修正设备类型映射，将简写名称转换为实际表名...');

    // 获取所有映射记录
    const mappingsQuery = `
      SELECT id, classification_code, naming_table, position_table, base_params_table
      FROM device_type_mappings
    `;

    const mappings = deviceParameterDb.db.prepare(mappingsQuery).all();
    console.log(`找到 ${mappings.length} 个设备类型映射`);

    // 更新映射表
    const updateMappingStmt = deviceParameterDb.db.prepare(`
      UPDATE device_type_mappings 
      SET 
        naming_table = ?,
        position_table = ?,
        base_params_table = ?,
        updated_at = datetime('now')
      WHERE id = ?
    `);

    let updatedCount = 0;
    
    for (const mapping of mappings) {
      // 将简写名称转换为实际表名
      const actualNamingTable = shortNameToTableName[mapping.naming_table] || mapping.naming_table;
      const actualPositionTable = shortNameToTableName[mapping.position_table] || mapping.position_table;
      const actualBaseParamsTable = shortNameToTableName[mapping.base_params_table] || mapping.base_params_table;
      
      // 只有当至少有一个表名需要更新时才执行更新
      if (actualNamingTable !== mapping.naming_table || 
          actualPositionTable !== mapping.position_table || 
          actualBaseParamsTable !== mapping.base_params_table) {
        
        try {
          updateMappingStmt.run(
            actualNamingTable,
            actualPositionTable,
            actualBaseParamsTable,
            mapping.id
          );
          
          console.log(`修正: ${mapping.classification_code}`);
          console.log(`  命名表: ${mapping.naming_table} -> ${actualNamingTable}`);
          console.log(`  位置表: ${mapping.position_table} -> ${actualPositionTable}`);
          console.log(`  参数表: ${mapping.base_params_table} -> ${actualBaseParamsTable}`);
          console.log('');
          
          updatedCount++;
        } catch (error) {
          console.error(`修正映射失败 ${mapping.classification_code}:`, error);
        }
      }
    }

    console.log(`\n成功修正了 ${updatedCount} 个设备类型映射`);
    console.log('映射修正完成！');

    // 验证修正结果
    const verifyStmt = deviceParameterDb.db.prepare(`
      SELECT classification_code, naming_table, position_table, base_params_table
      FROM device_type_mappings
      WHERE naming_table LIKE '表%' OR position_table LIKE '表%' OR base_params_table LIKE '表%'
      LIMIT 5
    `);
    const remainingIssues = verifyStmt.all();
    
    if (remainingIssues.length > 0) {
      console.log('\n还有以下映射使用简写名称：');
      remainingIssues.forEach((issue: any) => {
        console.log(`  ${issue.classification_code}: ${issue.naming_table}, ${issue.position_table}, ${issue.base_params_table}`);
      });
    } else {
      console.log('\n✅ 所有映射都已使用实际表名');
    }

  } catch (error) {
    console.error('修正设备映射失败:', error);
    process.exit(1);
  }
}

// 如果直接运行此脚本
if (require.main === module) {
  fixDeviceMappingsToRealTables();
}